package com.gs.zsmanage.web.controller;

import com.gs.zsmanage.entity.*;
import com.gs.zsmanage.service.*;
import com.gs.zsmanage.utils.HttpUtils;
import com.gs.zsmanage.utils.XmlParse;
import com.gs.zsmanage.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RstController {

    @Autowired
    UserService userService;
    @Autowired
    CustomWarningService customWarningService;
    @Autowired
    CustomLiveService customLiveService;
    @Autowired
    CustomMenuService customMenuService;
    @Autowired
    PdfFileService pdfFileService;
    @Autowired
    MonitorSiteService monitorSiteService;

    @RequestMapping("user/get")
    public Map userByDepartment(String phone, String password) {
        Map result = new HashMap();
        User user = userService.getUserByPhone(phone);
        if (user == null || !password.equals(user.getPassword())) {
            result.put("user", null);
            return result;
        }
        Map customMenu = customMenuService.getMenusAndIdsByDepartment(user.getDepartment());
        List<MenuVO> menuVOS = (List<MenuVO>) customMenu.get("menuVOS");
        List<CustomWarning> warnings = customWarningService.getCustomWarningByDepartment(user.getDepartment());
        List<CustomLive> customTypes = customLiveService.getAllDepartmentsAndStationType();
        List<String> needTypes = new ArrayList<>();
        for (CustomLive l : customTypes) {
            if (l.getDepartment().equals(user.getDepartment())) {
                needTypes.add(l.getStationType());
            }
        }
        List<CustomLive> customLives = new ArrayList<>();
        for (String stationType : needTypes) {
            CustomLive customLive = customLiveService.getCustomLiveByDepartmentAndStationType(user.getDepartment(), stationType);
            if (customLive != null && customLive.getElements() != null) customLives.add(customLive);
        }

        result.put("user", user);
        result.put("menu", menuVOS);
        result.put("warning", warnings);
        result.put("live", customLives);
        return result;
    }

    @RequestMapping("user/get/{department}")
    public List<User> userByDepartment(@PathVariable("department") String department) {
        List<User> users = userService.getUsersByDepartment(department);
        return users;
    }

    @RequestMapping("user/add")
    public ResponseEntity addUser(User user) {
        try {
            userService.addUser(user);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("user/update")
    public ResponseEntity delUser(User user) {
        try {
            userService.updateUser(user);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("user/del/{id}")
    public ResponseEntity delUser(@PathVariable("id") Integer id) {
        try {
            userService.deleteUser(id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("department/add")
    public ResponseEntity addDepartment(Department department) {
        try {
            userService.addDepartment(department);

            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("department/update")
    public ResponseEntity updateDepartment(Department department) {
        try {
            userService.updateDepartment(department);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("department/del/{id}")
    public ResponseEntity delDepartment(@PathVariable("id") Integer id) {
        try {
            userService.deleteDepartment(id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("menu/custom/{department}")
    public List<Integer> customMenu(@PathVariable("department") String name) {
        Map map = customMenuService.getMenusAndIdsByDepartment(name);
        List<Integer> customIds = (List<Integer>) map.get("customIds");
        return customIds;
    }

    @RequestMapping("menu/add/{department}/{menuId}")
    public ResponseEntity addDepartmentMenu(@PathVariable("department") String name, @PathVariable("menuId") Integer id) {
        try {
            customMenuService.addDepartmentMenu(name, id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

    @RequestMapping("menu/del/{department}/{menuId}")
    public ResponseEntity deleteDepartmentMenu(@PathVariable("department") String name, @PathVariable("menuId") Integer id) {
        try {
            customMenuService.deleteDepartmentMenu(name, id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("live/add/{liveId}/{elementId}")
    public ResponseEntity addLiveElement(@PathVariable("liveId") Integer liveId, @PathVariable("elementId") Integer elementId) {
        try {
            customLiveService.addLiveElement(liveId, elementId);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("live/batchAdd/{liveId}/{elementId}")
    public ResponseEntity batchAddLiveElement(@PathVariable("liveId") Integer liveId, @PathVariable("elementId") List<Integer> elementIds) {
        try {
            for (Integer elementId : elementIds) {
                customLiveService.addLiveElement(liveId, elementId);
            }
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("live/del/{liveId}/{elementId}")
    public ResponseEntity deleteLiveElement(@PathVariable("liveId") Integer liveId, @PathVariable("elementId") Integer elementId) {
        try {
            customLiveService.deleteLiveElement(liveId, elementId);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("live/batchDel/{liveId}/{elementId}")
    public ResponseEntity batchDeleteLiveElement(@PathVariable("liveId") Integer liveId, @PathVariable("elementId") List<Integer> elementIds) {
        try {
            for (Integer elementId : elementIds) {
                customLiveService.deleteLiveElement(liveId, elementId);
            }
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("live/update/{liveId}/{stations}")
    public void updateLiveStations(@PathVariable("liveId") Integer liveId, @PathVariable("stations") String stations) {
        if ("null".equals(stations)) {
            return;
        }
        customLiveService.updateLiveStations(liveId, stations);
    }

    @RequestMapping("live/visible/{liveId}/{visible}")
    public void updateLiveVisible(@PathVariable("liveId") Integer liveId, @PathVariable("visible") Integer v) {
        customLiveService.updateVisible(liveId, v);
    }


    @RequestMapping("warning/get/{department}")
    public List<String> updateLiveStations(@PathVariable("department") String name) {
        List<String> customNames = new ArrayList<>();
        List<CustomWarning> warnings = customWarningService.getCustomWarningByDepartment(name);
        for (CustomWarning warning : warnings) {
            customNames.add(warning.getName());
        }
        return customNames;
    }

    @RequestMapping("warning/add/{department}/{warningId}")
    public ResponseEntity addWarningCustom(@PathVariable("department") String name, @PathVariable("warningId") Integer id) {
        try {
            customWarningService.addCustomWarning(name, id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("warning/del/{department}/{warningId}")
    public ResponseEntity deleteCustomWarning(@PathVariable("department") String name, @PathVariable("warningId") Integer id) {
        try {
            customWarningService.deleteCustomWarning(name, id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //http://localhost/zs/file/ppt?year=2019
    //http://localhost/json/ppt/2019/2019春运春节天气预测（定稿）.ppt
    //http://localhost/json/importantreport/jcfw.json

    @RequestMapping("file/important")
    public List<String> importantReport(String startTime, String endTime) {
        return pdfFileService.listImportantFiles(startTime, endTime);
    }

    @RequestMapping("file/weekly")
    public List<String> weeklyReport(String startTime, String endTime) {
        return pdfFileService.listWeeklyFiles(startTime, endTime);
    }

    //http://localhost/zs/file/ppt?year=2019
    @RequestMapping("file/ppt")
    public List<String> pptReport(String year) {
        return pdfFileService.listPPTFiles(year);
    }

    @RequestMapping("alarm/get")
    public List<Alarm> getAlarms() throws Exception {
        return XmlParse.getAlarms();
    }

    @RequestMapping("monitorSite/add")
    public ResponseEntity addMonitorSite(MonitorSite monitorSite) {
        try {
            monitorSiteService.addMonitorSite(monitorSite);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("monitorSite/update")
    public ResponseEntity updateMonitorSite(MonitorSite monitorSite) {
        try {
            monitorSiteService.updateMonitorSite(monitorSite);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("monitorSite/del/{id}")
    public ResponseEntity delMonitorSite(@PathVariable("id") Integer id) {
        try {
            monitorSiteService.deleteMonitorSite(id);
            return HttpUtils.buildOKResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return HttpUtils.buildStringResponse("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
